package com.cater.examhelper.listener;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;

import com.cater.examhelper.R;
import com.cater.examhelper.listener.MockExamGestureListener.OnFlingListener;

public class MockExamViewFlipper extends ViewFlipper implements OnFlingListener
{
	private GestureDetector mGestureDetector = null;
	private OnViewFlipperListener mOnViewFlipperListener = null;

	public MockExamViewFlipper(Context context)
	{
		super(context);
	}
	
	public MockExamViewFlipper(Context context, AttributeSet att)
	{
		super(context,att);
	}
	
	public void setmOnViewFlipperListener(OnViewFlipperListener mOnViewFlipperListener)
	{
		this.mOnViewFlipperListener = mOnViewFlipperListener;
		MockExamGestureListener mockExamGestureListener = new MockExamGestureListener();
		mockExamGestureListener.setOnFlingListener(this);
		mGestureDetector = new GestureDetector(mockExamGestureListener);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev)
	{
		if (null != mGestureDetector)
		{
			return mGestureDetector.onTouchEvent(ev);
		}
		else
		{
			return super.onInterceptTouchEvent(ev);
		}

	}

	@Override
	public void flingToNext()
	{
		if(null != mOnViewFlipperListener)
		{
			int childCnt = getChildCount();
			if(childCnt == 2)
			{
				removeViewAt(1);
			}
			addView(mOnViewFlipperListener.getNextView(),0);
			if(0 != childCnt)
			{
				
				setInAnimation(getContext(), R.anim.animation_right_in);
				setOutAnimation(getContext(), R.anim.animation_left_out);
				setDisplayedChild(0);
			}
		}
	}

	@Override
	public void flingToPrevious()
	{
		if(null != mOnViewFlipperListener)
		{
			int childCnt = getChildCount();
			if(childCnt == 2)
			{
				removeViewAt(1);
			}
			addView(mOnViewFlipperListener.getPreviousView(),0);
			if(0 != childCnt)
			{
				setInAnimation(getContext(), R.anim.animation_left_in);
				setOutAnimation(getContext(), R.anim.animation_right_out);
				setDisplayedChild(0);
			}
		}
	}

	public interface OnViewFlipperListener
	{
		public View getNextView();

		public View getPreviousView();
	}
	
}
